<?php
namespace app\common\model;
class Report extends \think\Model
{
    protected $resultSetType = 'collection';
    public function inserts(){
        $data = input('param.');
        $validate = validate($this -> name);
        if(!$validate->check($data))
            return [
                'message' => $validate->getError()
            ];
        $saveData['name'] = $data['name'];
        $saveData['telephone'] = $data['telephone'];
        $saveData['building'] = $data['building'];
        $saveData['date'] = time();
        $saveData['words'] = $data['words'];
        $saveData['origin'] = $data['origin'];
        $saveData['account_id'] = $data['account_id']?:0;
        $this -> save($saveData);

        //发送短信给管理员
        $fangsale = \app\common\model\Building::where('company','in',['领郡置业有限公司','房金时代网络科技有限公司'])->column('name');
        $fangsale = array_merge($fangsale,[
            '隆基泰和万和郡',
            '房金时代首页'

        ]);


        $telephone = [
            '18829343042',
            '18829353331',
            '18191027139',
            '18392320318',
        ];

        //房金发送给张
        if (in_array($data['building'],$fangsale)){
            $telephone[] = '15319702280';
        }

        //发送给个人
        if ($data['account_id']){
            $telephone = [];
            $accountInfo = \app\common\model\Account::where('id',$data['account_id'])->find();
            $telephone[] = $accountInfo['telephone'];
            //如果是全民，发送给管理者
            if ($accountInfo['role'] =='33'){
                $higher = $accountInfo -> higher -> a_id;
                $higherInfo = \app\common\model\Account::where('id',$higher)->find();
                $telephone[] = $higherInfo['telephone'];
            }
        }



        $send = new \app\common\logic\Send();
        $WxMini =new \app\common\logic\WxMini();
        foreach ($telephone as $value){
            //发送短信通知
             $send -> inform($value,$data['building'],$data['name'],$data['telephone']);
//            发送小程序通知
            $open_id = \app\common\model\Account::where('telephone',$value)->find()->open_id;
            if ($open_id){
                $sendData = [
                    'keyword1' => ['value' => $data['name']],
                    'keyword2' => ['value' => $data['telephone']],
                    'keyword3' => ['value' => $data['building']],
                    'keyword4' => ['value' => date('Y-m-d H:i:s')],
                ];
                $WxMini -> sendNotify($open_id,4,'pages/index/index',$sendData);
            }
        }

        return [
            'code' => '1',
            'message' => '报备成功'
        ];
    }
}